package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.epc.dao.MaintenanceWorkbenchMapper;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZwxTenantFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.MaintenanceWorkbenchService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 维修师傅（工作台）
 * <p>
 *
 * @author: zhoucong1
 * @create: 2019-04-09 12:00
 **/
@Service
public class MaintenanceWorkbenchServiceImpl  implements MaintenanceWorkbenchService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(RepairEvaServiceImpl.class);

    @Autowired
    MaintenanceWorkbenchMapper maintenanceWorkbenchMapper;

    @Autowired
    ZzuulFeign zzuulFeign;
    @Autowired
    ZpcMktFeign zpcMktFeign;

    @Autowired
    ZwxTenantFeign zwxTenantFeign;

    /**
    * <p>
    * 重要状态提醒
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult importantState(){
        JsonResult result = new JsonResult();
        Map<String,Object> params = maintenanceWorkbenchMapper.getImportantState();
        if (params == null||params.size()<1){
            result.putFailed("服务器繁忙...");

        }
        return result.put(params);
    }


    /**
     * 维修工单的数量
     */
    public Integer getRepairCount(){
        return maintenanceWorkbenchMapper.getRepairCount();
    }

    /**
     * <p>
     * 维修工单池
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @Override
    public List<Map<String,Object>> getRepairList(Map<String,Object> params) {

        List<Map<String, Object>> epcList = maintenanceWorkbenchMapper.getRepairList(params);
        if (epcList.size() > 0) {
            //保存维修工单的房源编号
            List<String> HouConList = new ArrayList<String>();
            //保存管家报修人编号
            List<String> SysUserId = new ArrayList<String>();
            //保存客户报修人编号
            List<String> tidList = new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null) {
                    //根据 HouId查询房源信息
                    HouConList.add(map.get("HouCon").toString());
                }
                if (map.get("RepType") != null) {
                    //报修人类型 1.普通用户，2.管家
                    //是否通过管家巡房发起  0.否   1是'
                    if (map.get("RepType").toString().equals("2")) {
                        if (map.get("RepairsId") != null) {
                            SysUserId.add(map.get("RepairsId").toString());
                        }
                    } else {
                        if (map.get("RepairsId") != null) {
                            tidList.add(map.get("RepairsId").toString());
                        }
                    }
                }
            }
            System.out.println("传递到网关的参数:" + SysUserId);

            if (SysUserId.size() > 0) {//如果报修人是管家的 编号 集合大于0就去网关查询
                params.put("SysUserId", SysUserId);
                //JsonResult zuulResult = zzuulFeign.getRuserList(params);
                JsonResult zuulResult =zzuulFeign.getLoginName(params);
                List<Map<String, Object>> zuulList = (List<Map<String, Object>>) zuulResult.get("result");
                if (zuulResult.get("errorCode").equals("0")) {
                    for (Map<String, Object> map : epcList) {
                        for (Map<String, Object> map2 : zuulList) {
                            if (MapUtils.getString(map, "RepairsId") != null && MapUtils.getString(map2, "user_id") != null && MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                }
            }
            if(tidList.size()>0){//报修人是租客
                params.put("tidList",tidList);
                JsonResult tenantResult = zwxTenantFeign.getTenantList(params);
                List<Map<String,Object>> tenantList = (List<Map<String, Object>>) tenantResult.get("result");
                for (Map<String,Object> map1:epcList) {
                    for (Map<String,Object> map2:tenantList) {
                        if (MapUtils.getString(map1, "RepairsId") != null && MapUtils.getString(map2, "TenId") != null && MapUtils.getString(map1, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                            map1.putAll(map2);
                        }
                    }
                }
            }
          if (HouConList.size()>0){//房源信息
              params.put("HouConList",HouConList);
              JsonResult result = zpcMktFeign.getHouConList(params);
              List<Map<String, Object>> resultList = (List<Map<String, Object>>) result.get("result");
              if (result.get("errorCode").equals("0")) {
                  for (Map<String, Object> map : epcList) {
                      for (Map<String, Object> map2 : resultList) {
                          if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                              map.putAll(map2);
                          }
                      }
                  }
              }

          }

        }
        return epcList;
    }

    /**
    * <p>
    * 查询维修工单评价的数量
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public Integer getRepairEvaCount(Map<String,Object> params){
        return maintenanceWorkbenchMapper.getRepairEvaCount(params);
    }


    /**
     * 查询所有维修工单评价
     * @author: zhoucong
     * @param params
     * @return
     */
    @Override
    public List<Map<String,Object>> getRepairEvaList(Map<String, Object> params) {
        List<Map<String, Object>> epcList = maintenanceWorkbenchMapper.getRepairEvaList(params);
        if (epcList.size() > 0) {
            //保存维修工单的房源编号
            List<String> HouConList = new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null && !map.get("HouCon").equals("")) {
                    HouConList.add(MapUtils.getString(map, "HouCon"));
                }
                if (map.get("WxUser")!=null){
                  map.put("TenId",map.get("WxUser"));
                  JsonResult zwxTenant = zwxTenantFeign.getTenantTenPhone(map);
                  Map<String,Object> zwxMap = (Map<String, Object>) zwxTenant.get("result");
                  if(zwxMap.get("TenId") !=null&&map.get("WxUser")!=null&&MapUtils.getString(zwxMap,"TenId").equals(MapUtils.getString(map,"TenId"))){
                      map.putAll(zwxMap);
                  }
                }
            }

            params.put("HouConList", HouConList);
            //根据房源编号到市场部查询房源基本信息
            JsonResult result = zpcMktFeign.getHouConList(params);
            List<Map<String, Object>> resultList = (List<Map<String, Object>>) result.get("result");
            if (result.get("errorCode").equals("0")) {
                for (Map<String, Object> map : epcList) {
                    for (Map<String, Object> map2 : resultList) {
                        if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }
        }
    return epcList;
    }



}
